package kau.step06_miniProject;
//지혜수정
import java.util.ArrayList;
import java.io.IOException;
import java.io.Reader;
import java.sql.*;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import util.MyUtility;

import exception.*;

import dto.HeadOffice;

public class OfficeDAO  {
	private static SqlMapClient sqlMapper;
	static {
		try {
			Reader reader = Resources.getResourceAsReader("kau/step06_miniProject/SqlMapConfig.xml");
			sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
			reader.close(); 
		} catch (IOException e) {
			throw new RuntimeException("xml문서 read해서 SqlMapClient객체에 저장시 에러" + e,  e);
		}
	}	  

//기민수정
	/**
	 *\
	 *@param office HeadOffice 
	 * @throws SQLException 
	 */
	public static void insert(HeadOffice office) throws DuplicateException{
		try {

			sqlMapper.insert("Office.insertAll", office);
			

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	} 

	public static ArrayList<HeadOffice> getOfficeList() throws SQLException{

		return (ArrayList) sqlMapper.queryForList("Office.selectAll");
	}

	public static HeadOffice getOfficeByID(int businessNo)  throws RecordNotFoundException, SQLException{

		return (HeadOffice) sqlMapper.queryForObject("Office.selectByID", businessNo);
	}


	public static void update(HeadOffice office)  throws RecordNotFoundException, SQLException{

		sqlMapper.update("Office.updateAll",office);
	}

	public static void delete(int businessNo)  throws RecordNotFoundException, SQLException{
		sqlMapper.delete("Office.delete", businessNo);
	}

	public static ArrayList<HeadOffice> getOfficeByName(String officeName) throws RecordNotFoundException, SQLException{
		return  (ArrayList<HeadOffice>) sqlMapper.queryForList("Office.selectByName" , officeName);
	}
	
	public static void main(String[] args) throws SQLException {
	
		try {
			//TODO: insert 부분 
			insert(new HeadOffice(1,"삼성","수원","심지성","1980-4-9"));
			insert(new HeadOffice(2,"LG","성남","구본준","1981-5-9"));
			insert(new HeadOffice(3,"SK","강남","강남길","1982-6-9"));
			
		}catch (DuplicateException e) {
			System.out.println(e.getMessage());
		}

		System.out.println("\t<<모든 지점정보 ["+MyUtility.getCurrentDate()+"]>>");

		//TODO: 모든 지점 정보 출력 
		System.out.println(getOfficeList());

		System.out.println("\n========= 사업자번호 3인곳  검색 ===========");
		try {
			//TODO: 비지니스 ID로 검색 하는 쿼리 출력
			System.out.println(getOfficeByID(3));
			System.out.println("\n============ 사업자번호 3인곳 수정 후 검색 ============");
			//TODO: 비지니스 ID로 업데이트 출력 
			update(new HeadOffice(3,"KT","서울","백욱진","1991-1-3"));
			System.out.println(getOfficeList());
			System.out.println("\n====== 사업자번호 3인곳 지점 삭제 후 지사목록 ========");
			//TODO: 해당 비지니스 ID로 삭제 하고 전체 출력
			delete(3);
			System.out.println(getOfficeList());
			System.out.println("\n===========LG 이름으로 검색=============");
			System.out.println(getOfficeByName("LG"));
			
		}catch (RecordNotFoundException e) {
			System.out.println(e.getMessage());
		}
	}

}
